﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Text.RegularExpressions; 

namespace MyGame2
{
    public partial class frmOption : Form
    {
        public frmOption()
        {
            InitializeComponent();
        }

        private void btnDetermine_Click(object sender, EventArgs e)
        {
            if (CheckString(this.txtBox.Text.Trim()))
            {
                MessageBox.Show("你输入的方块大小不对", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            MyConfig command = new MyConfig();
            //1是 0否
            //是否允许问号
            command.IsQuestion = cboIsQuestion.Checked ? "1" : "0";
            //是否播放声音
            command.PlaySound = cboPlaySound.Checked ? "1" : "0";
            //是否允许点击X次一定不是雷
            command.FirstPlay = cboNewPlay.Checked ? "1" : "0";
            //是否翻开X个空白方块
            command.NewPlay = cboNewPlay.Checked ? "1" : "0";
            //小方块大小
            command.Box = txtBox.Text;
            //翻开前方块的背景颜色
            command.BackColor = ColorTranslator.ToHtml(plBackColor.BackColor);
            //翻开后方块的背景颜色
            command.FlipColor = ColorTranslator.ToHtml(plFlipColor.BackColor);

            if (rdobtnInitial.Checked)
            {
                command.Height = "9";
                command.Width = "9";
                command.MineCount = "10";
            }
            else if (rdobtnMiddle.Checked)
            {
                command.Height = "16";
                command.Width = "16";
                command.MineCount = "40";
            }
            else if (rdobtnSenior.Checked) 
            {
                command.Height = "16";
                command.Width = "30";
                command.MineCount = "99";
            }
            else 
            {
                if (CheckString(this.txtHeight.Text.Trim()) || CheckString(this.txtWidth.Text.Trim()) || CheckString(this.txtMine.Text.Trim())) 
                {
                    MessageBox.Show("你输入的自定义参数不是有效数字", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                command.Height = this.txtHeight.Text.Trim();
                command.Width = this.txtWidth.Text.Trim();
                command.MineCount = this.txtMine.Text.Trim();
            }
            List<MyConfig> list = new List<MyConfig>(1);
            list.Add(command);
            XmlHelper.XmlSerializeToFile(list, XmlFileName, Encoding.UTF8);
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void rdobtnCustom_CheckedChanged(object sender, EventArgs e)
        {
            if (rdobtnCustom.Checked)
            {
                this.txtHeight.Enabled = true;
                this.txtMine.Enabled = true;
                this.txtWidth.Enabled = true;
                lblHeight.Enabled = true;
                lblMine.Enabled = true;
                lblWidth.Enabled = true;
            }
            else 
            {
                this.txtHeight.Enabled = false;
                this.txtMine.Enabled = false;
                this.txtWidth.Enabled = false;
                lblHeight.Enabled = false;
                lblMine.Enabled = false;
                lblWidth.Enabled = false;
            }
        }

        private static readonly string XmlFileName = Application.StartupPath + "\\GameConfig.xml";
        private void frmOption_Load(object sender, EventArgs e)
        {
            List<MyConfig> list = XmlHelper.XmlDeserializeFromFile<List<MyConfig>>(XmlFileName, Encoding.UTF8);
            int x = int.Parse(list[0].Width);
            int y = int.Parse(list[0].Height);
            txtBox.Text = list[0].Box;
            int mineCount = int.Parse(list[0].MineCount);

            cboNewPlay.Checked = list[0].NewPlay == "1" ? true : false;
            cboFrist.Checked = list[0].FirstPlay == "1" ? true : false;
            cboPlaySound.Checked = list[0].PlaySound == "1" ? true : false;
            cboIsQuestion.Checked = list[0].IsQuestion == "1" ? true : false;

            plBackColor.BackColor = ColorTranslator.FromHtml(list[0].BackColor);
            plFlipColor.BackColor = ColorTranslator.FromHtml(list[0].FlipColor);
            if (x == 9 && y == 9 && mineCount == 10)
            {
                rdobtnInitial.Checked = true;
            }
            else if (x == 16 && y == 16 && mineCount == 40)
            {
                rdobtnMiddle.Checked = true;
            }
            else if (x == 30 && y == 16 && mineCount == 99)
            {
                rdobtnSenior.Checked = true;
            }
            else
            {
                rdobtnCustom.Checked = true;
                this.txtHeight.Text = list[0].Height;
                this.txtWidth.Text = list[0].Width;
                this.txtMine.Text = list[0].MineCount;
            }
            rdobtnCustom_CheckedChanged(null, null);
        }

        public bool CheckString(string str) 
        {
            int a = 0;
            if (str == string.Empty || str == null || !int.TryParse(str,out a)) 
            {
                return true;
            }
            return false;
        }

        private void plBackColor_Click(object sender, EventArgs e)
        {
            this.colorDialog1.AllowFullOpen = true;
            this.colorDialog1.AnyColor = true;
            this.colorDialog1.FullOpen = true;
            this.colorDialog1.SolidColorOnly = false;
            this.colorDialog1.ShowDialog();
            this.plBackColor.BackColor = this.colorDialog1.Color;
        }

        private void plFlipColor_Click(object sender, EventArgs e)
        {
            this.colorDialog1.AllowFullOpen = true;
            this.colorDialog1.AnyColor = true;
            this.colorDialog1.FullOpen = true;
            this.colorDialog1.SolidColorOnly = false;
            this.colorDialog1.ShowDialog();
            this.plFlipColor.BackColor = this.colorDialog1.Color;
        }

        private void btnRdSetting_Click(object sender, EventArgs e)
        {
            MyConfig command = new MyConfig();
            command.IsQuestion = "0";
            command.PlaySound = "1";
            command.Box = "24";
            command.Height = "16";
            command.Width = "30";
            command.MineCount = "99";
            command.FlipColor = ColorTranslator.ToHtml(Color.White);
            command.BackColor = ColorTranslator.ToHtml(Color.SkyBlue);
            command.NewPlay = "0";
            command.FirstPlay = "0";
            List<MyConfig> list = new List<MyConfig>(1);
            list.Add(command);
            XmlHelper.XmlSerializeToFile(list, XmlFileName, Encoding.UTF8);
            this.Close();
        }

        private void rdobtnInitial_Click(object sender, EventArgs e)
        {
            if (rdobtnInitial.Checked)
            {
                cboNewPlay.Text = "开局翻开1个空白格子";
                cboFrist.Text = "首次点击方块一定不爆炸";
            }
            else if (rdobtnMiddle.Checked)
            {
                cboNewPlay.Text = "开局翻开2个空白格子";
                cboFrist.Text = "前2点击方块一定不爆炸";
            }
            else 
            {
                cboNewPlay.Text = "开局翻开3个空白格子";
                cboFrist.Text = "前3次点击方块一定不爆炸";
            }
        }
    }
}
